package com.lzh.others.basejava.IO_18;

import java.io.File;
import java.io.FilenameFilter;
import java.net.URL;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DirList {

    public static void main(String[] args) {

        String name = "DirList.class"; // 需要检测的字符串

        Pattern pattern = Pattern.compile("\\.class$");

        Matcher matcher = pattern.matcher(name);

        if (matcher.matches()) {
            System.out.println("The string '" + name + "' ends with '.class'.");
        } else {
            System.out.println("The string '" + name + "' does not end with '.class'.");
        }


        Class<?> clazz = DirList.class;
        URL resource = clazz.getResource(clazz.getSimpleName() + ".class");
        File path = new File(resource.getPath()).getParentFile();
        System.out.println("packageDir = " + path.getAbsolutePath());
        String[] list;
        if (args.length == 0) {
            list = path.list();
        } else {
            list = path.list(new DriFilter("\\.class$"));
        }
        Arrays.sort(list, String.CASE_INSENSITIVE_ORDER);
        for (String s : list) {
            System.out.println("s = " + s);
        }
    }
}

class DriFilter implements FilenameFilter {
    private Pattern pattern;

    public DriFilter(String regex) {
        pattern = Pattern.compile(regex);
    }

    @Override
    public boolean accept(File dir, String name) {
        String string = pattern.toString();
        System.out.println("string = " + string);
        System.out.println("name = " + name);
        Matcher matcher = pattern.matcher(name);
        boolean matches = matcher.matches();
        System.out.println("matches = " + matches);
        return pattern.matcher(name).matches();
    }
}